clear all
set memo 100m
capture log close

local filename postCMC

log using postCMC, replace text

local output_dir draft
local output_tex1 `filename'.tex

cd "C:\Users\rujia\Dropbox\Replication\treatyports"

use data/TreatyPorts_BasePanel.dta, clear


qui tabu id, gen(idummy)
qui tabu year, gen(ydummy)


tsset id  year
bysort id: gen rank=_n
order rank
by id: gen AnnualGrowth=(hpopu[2]-hpopu[1])/hpopu[1]/44 if rank<=2
order AnnualGrowth
by id: replace AnnualGrowth=100*(hpopu[3]-hpopu[2])/hpopu[2]/31 if rank==3
by id: replace AnnualGrowth=100*(hpopu[4]-hpopu[3])/hpopu[3]/29 if rank==4
by id: replace AnnualGrowth=100*(hpopu[5]-hpopu[4])/hpopu[4]/30 if rank==5
by id: replace AnnualGrowth=100*(hpopu[6]-hpopu[5])/hpopu[5]/43 if rank==6
by id: replace AnnualGrowth=100*(hpopu[7]-hpopu[6])/hpopu[6]/11 if rank==7
by id: replace AnnualGrowth=100*(hpopu[8]-hpopu[7])/hpopu[7]/18 if rank==8
by id: replace AnnualGrowth=100*(hpopu[9]-hpopu[8])/hpopu[8]/8 if rank==9
by id: replace AnnualGrowth=100*(hpopu[10]-hpopu[9])/hpopu[9]/10 if rank==10

sum AnnualGrowth, de
gen post_cmc=(year>cmcyear)

replace coastal2=0 if coastal2==.
replace yangtze2=0 if yangtze2==.
forvalues x=1(1) 10 {
gen coastal_`x'=coastal2*ydummy`x'
gen yangtze_`x'=yangtze2*ydummy`x'
gen latitude_`x'=latitude*ydummy`x'
gen longitude_`x'=longitude*ydummy`x'
gen langtax_`x'=landtax*ydummy`x'
gen silk_`x'=silk*ydummy`x'
gen tea_`x'=tea*ydummy`x'
gen grandcanal_`x'=dis_yunhe*ydummy`x'
gen taipingr_`x'=taipingrrebellion*ydummy`x'

}


label var AnnualGrowth "Annual Growth"
label var post_cmc "Post CMC"
local fe "idummy* ydummy*"

local control_fe "coastal_* yangtze_* latitude_* longitude_* silk_* tea_* grandcanal_* taipingr_*"

#delimit ;
eststo clear;
eststo:  regress AnnualGrowth post_cmc;
estadd scalar rsquare=e(r2);
estadd scalar nobs=e(N);

eststo:  regress AnnualGrowth post_cmc `fe', noconstant vce(cluster id);
estadd local fe "Y";
estadd scalar rsquare=e(r2);
estadd scalar nobs=e(N);


eststo: regress AnnualGrowth post_cmc `fe'  `control_fe', noconstant vce(cluster id);
estadd local fe "Y";
estadd local control_fe "Y";
estadd scalar rsquare=e(r2);
estadd scalar nobs=e(N);


eststo:  regress AnnualGrowth post_cmc `fe'  `control_fe' if wave!=1, noconstant vce(cluster id);
estadd local fe "Y";
estadd local control_fe "Y";
estadd scalar rsquare=e(r2);
estadd scalar nobs=e(N);


esttab using draft/`output_tex1', replace
	width(\hsize) 
	mlabel (, depvar)
	b(3)
	se(3)
	noconstant
	star(* 0.10 ** 0.05 *** 0.01)
	keep(
post_cmc
		)
		
	label
	stats(
		fe
		control_fe
		 nobs
		 rsquare
		, 
		labels(
			"Prefecture and Year FE" 
			"Controls*Year FE" 
			"\# Observations" 
			"R-squared"
			) 
		fmt(0)
		layout(@)
		)
	nonotes
	;
